﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace SwimmingCompetitions
{
    public partial class ResultsView : UserControl
    {
        public ResultsView()
        {
            InitializeComponent();
        }

        private DataTable _tablePersonal;
        private DataTable _tableTeam;

        private void reset()
        {
            _tablePersonal = new DataTable();
            _tablePersonal.Columns.Add("Участник");
            _tablePersonal.Columns.Add("Очки");
            _dataGridViewPersonal.DataSource = _tablePersonal;

            _tableTeam = new DataTable();
            _tableTeam.Columns.Add("Команда");
            _tableTeam.Columns.Add("Очки");
            _dataGridViewTeam.DataSource = _tableTeam;
        }

        private static int sumScore(string key, int keyColumn, DataTable race)
        {
            var sum = 0;
            foreach (DataRow row in race.Rows)
            {
                if (row.ItemArray[keyColumn].Equals(key))
                {
                    var r = row.ItemArray[CompetitionModel.ColumnScore];
                    sum += (r is DBNull)
                               ? 0
                               : (int) row.ItemArray[CompetitionModel.ColumnScore];
                }
            }
            return sum;
        }

        private static IEnumerable<KeyValuePair<string, int>> calculateTeamResults(CompetitionModel model)
        {
            return model.Teams
                .Select(team =>
                        new KeyValuePair<string, int>(team, model.Races
                                                                .Sum(
                                                                    race =>
                                                                    sumScore(team, CompetitionModel.ColumnTeam,
                                                                             race.Table))));
        }

        private IEnumerable<KeyValuePair<string, int>> calculatePersonalResults(CompetitionModel model)
        {
            return model.Participants
                .Select(participant =>
                        new KeyValuePair<string, int>(participant, model.Races
                                                                       .Sum(
                                                                           race =>
                                                                           sumScore(participant,
                                                                                    CompetitionModel.ColumnParticipant,
                                                                                    race.Table))));
        }

        public void Initialize(CompetitionModel model)
        {
            reset();
            foreach (var result in calculatePersonalResults(model).OrderByDescending(x => x.Value))
            {
                _tablePersonal.Rows.Add(result.Key, result.Value);
            }
            foreach (var result in calculateTeamResults(model).OrderByDescending(x => x.Value))
            {
                _tableTeam.Rows.Add(result.Key, result.Value);
            }
        }

        private void buttonPrintResultsClick(object sender, EventArgs e)
        {
            using (var pub = new TablePublisher(_tablePersonal, "Личное первенство", CompetitionMode.Current.GetVisualModel()))
            {
                pub.PrintPreview();
            }
            using (var pub = new TablePublisher(_tableTeam, "Командное первенство", CompetitionMode.Current.GetVisualModel()))
            {
                pub.PrintPreview();
            }
        }
    }
}
